<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/10/16
 * Time: 10:29
 */
namespace app\admin\service;
use app\models\CourseLogModel;
use app\models\PayFlowModel;
use app\models\ReceivablesModel;
use app\models\StudentModel;

/**
 * Class FinanceService
 * @package app\admin\service
 */
class FinanceService
{
    /**
     * @return array
     */
    public function capital_flow()
    {
        try {
            $page = input('page', 1);
            $listRows = input('limit', 5);
            $day = input('day', '');
            $student_name = input('student_name', '');
            $contract_number = input('contract_number', '');
            $receipt_number = input('receipt_number', '');
            $type = input('type', 'all');
            if($type == ""){
                $type = 'all';
            }
            $where = [];
            if($type == 'all'){
                $where['type'] = ['in','1,2,3,4'];
            }else{
                $where['type'] = $type;
            }

            /*
            $StudentModel = new StudentModel();
            if($student_name){
                $contract_number_arr = '';
                $student_result = $StudentModel->where(['name' => $student_name])->select();
                foreach ($student_result as  $key => $value){
                    $contract_number_arr = $contract_number_arr.$value['contract_number'].',';
                }
                $contract_number_arr = substr($contract_number_arr,0,strlen($contract_number_arr)-1);
                $where['contract_number'] = ['in',$contract_number_arr];
            }
            */


            if ($day) {
                $date = explode(' - ', $day);
                $start = $date[0] . ' 00:00:00';
                $end = $date[1] . ' 23:59:59';
                $where['paydate']=  [['egt', $start], ['lt', $end]];
            }
            if ($contract_number) $where['contract_number'] = ['like','%'.$contract_number.'%'];
            if ($receipt_number) $where['receipt_number'] = ['like','%'.$receipt_number.'%'];


            $order = 'id desc';

			if(get_current_type() == 4 || get_current_type() == 5){
				$where['campus_id'] = get_current_campus_id();
			}elseif (get_current_type() == 2 || get_current_type() == 3){
				$where['campus_id'] = ['in',get_current_campus_id()];
			}

            return PayFlowModel::pageLists($where, $page, $listRows, $order);
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    /**
     * @return array
     */
    public function already_reported_course()
    {
        try {
            $page = input('page', 1);
            $listRows = input('limit', 5);
            $contract_number = input('contract_number', '');
            $campus_id = input('campus_id', '');
            $record_status = input('record_status', '');
            $grades_id = input('grades_id','');
            $course_id = input('course_id', '');
            $start_date = input('start_date', '');
            $end_date = input('end_date', '');
            $type = input('type','all');
            $where = [];
            if ($contract_number) $where['contract_number'] = $contract_number;

            if ($campus_id) $where['campus'] = $campus_id;

            if ($grades_id) $where['grades_id'] = $grades_id;

            if ($record_status) $where['record_status'] = $record_status;

            if ($course_id) $where['course'] = ['like', '%'.$course_id.'%'];;

            $where_date = [];
            if($start_date && $end_date){
                $where_date['start_date'] = $start_date;
                $where_date['end_date'] = $end_date;
            }

			/*
			if($type == 'all'){
                $where['status'] = ['in','1,2'];
            }else{
                $where['status'] = $type;
            }
			*/
			
			if($type == 'all'){
                $where['status'] = ['in','1,2,3'];  //全部
            }else if($type == 'stay'){
                $where['status'] = ['in','2']; //未收款
            }else if($type == 'part'){  //部分收款
                $where['status'] = ['in','3'];
            }

			if(get_current_type() == 4 || get_current_type() == 5){
				$where['campus'] = get_current_campus_id();
			}elseif (get_current_type() == 2 || get_current_type() == 3){
				$where['campus'] = ['in',get_current_campus_id()];
			}
            $order = 'id desc';
            return CourseLogModel::pageLists($where, $page, $listRows, $order,$where_date);
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    /**
     * @return array
     */
    public function receivables_management()
    {
        try {
            $page = input('page', 1);
            $listRows = input('limit', 5);
            $contract_number = input('contract_number', '');
            $campus_id = input('campus_id', '');
            $receipt_number = input('receipt_number', '');
            $start_date = input('start_date', '');
            $end_date = input('end_date', '');
       //     $paydate = input('paydate', '');
            $where = [];
            if ($contract_number) $where['contract_number'] = $contract_number;
            if ($campus_id) $where['campus_id'] = $campus_id;
            if ($receipt_number) $where['receipt_number'] = trim($receipt_number);
        //    if ($paydate) $where['paydate'] = $paydate;

            if($start_date && $end_date){
                //收款日期
                $where['paydate'] = array('between', array($start_date,$end_date));

            }
			if(get_current_type() == 4 || get_current_type() == 5){
				$where['campus_id'] = get_current_campus_id();
			}elseif (get_current_type() == 2 || get_current_type() == 3){
				$where['campus_id'] = ['in',get_current_campus_id()];
			}
            $order = 'id desc';
            return ReceivablesModel::pageLists($where, $page, $listRows, $order);
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    public function receivables_check_status()
    {
        try {
            $data = input('post.');
            if($data['status'] == 2){
                $msg = '审核通过';
            }else{
                $msg = '审核不通过';
            }
            ReceivablesModel::update($data);
            return ['code' => 0, 'msg' => $msg];
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }
}